package jsonParser;

import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class JsonParserUtil {
    
    private static final String NULL_STRING = "null";
    
    public static String getString(String name, JSONObject json) {
        
        try {
            
            if (json.isNull(name)) {
                return null;
            } else {
                return json.getString(name);
            }
            
        } catch (Exception jsone) {
            return null;
        }
    }
    
    public static JSONArray getJSONArray(String name, JSONObject json) {
        
        try {
            
            if (name != null && json != null) {
                
                if (json.isNull(name)) {
                    
                    return null;
                    
                } else {
                    
                    return json.getJSONArray(name);
                    
                }
                
            } else {
                return null;
            }
            
        } catch (Exception jsone) {
            return null;
        }
    }
    
    public static Boolean getBoolean(String name, JSONObject json) {
        
        try {
            
            if (json.isNull(name)) {
                
                return false;
                
            } else {
                
                return json.getBoolean(name);
                
            }
            
        } catch (Exception e) {
            return false;
        }
        
    }
    
    public static JSONObject getObject(String name, JSONObject json) {
        
        try {
            
            if (json.isNull(name)) {
                
                return null;
                
            } else {
                
                return json.getJSONObject(name);
                
            }
            
        } catch (Exception e) {
            return null;
        }
    }
    
    public static int getInt(String name, JSONObject json) {
        
        return getInt(getString(name, json));
    }
    
    public static int getInt(String str) {
        
        if (TextUtils.isEmpty(str) || NULL_STRING.equals(str)) {
            return 0;
        } else {
            
            try {
                return Integer.parseInt(str);
            } catch (Exception nfe) {
                return 0;
            }
            
        }
    }
    
    public static List<String> getArrayStr(String name, JSONObject json) {
        
        List<String> strs = new ArrayList<String>();
        
        try {
            
            JSONArray jsonArray = json.getJSONArray(name);
            
            for (int i = 0; i < jsonArray.length(); i++) {
                
                String str = jsonArray.getString(i);
                strs.add(str);
                
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return strs;
    }
    
    public static int getIntDefaultMinus(String name, JSONObject json) {
        
        String str = getString(name, json);
        
        if (TextUtils.isEmpty(str) || NULL_STRING.equals(str)) {
            return -1;
        } else {
            
            try {
                return Integer.parseInt(str);
            } catch (Exception nfe) {
                return -1;
            }
            
        }
    }
    
    public static long getLong(String name, JSONObject json) {
        
        return getLong(getString(name, json));
    }
    
    public static long getLong(String str) {
        
        if (TextUtils.isEmpty(str) || NULL_STRING.equals(str)) {
            return 0;
        } else {
            try {
                return Long.parseLong(str);
            } catch (Exception nfe) {
                return 0;
            }
        }
        
    }
    
    public static float getFloat(String name, JSONObject json) {
        
        return getFloat(getString(name, json));
    }
    
    public static float getFloat(String str) {
        
        if (TextUtils.isEmpty(str) || NULL_STRING.equals(str)) {
            return 0.0f;
        } else {
            try {
                return Float.valueOf(str);
            } catch (Exception nfe) {
                return 0.0f;
            }
        }
        
    }
    
    public static ArrayList<String> getScreenShot(String name, JSONObject json) {
        
        ArrayList<String> screenShotsList = null;
        String screenShotString = null;
        
        if (TextUtils.isEmpty(name) || NULL_STRING.equals(name)) {
            return null;
        } else {
            
            try {
                
                screenShotString = json.getString(name);
                
                if (screenShotString != null) {
                    
                    String[] arrayStr = screenShotString.split("###");
                    
                    if (arrayStr != null && arrayStr.length > 0) {
                        
                        int size = arrayStr.length;
                        screenShotsList = new ArrayList<String>();
                        
                        for (int i = 0; i < size; i++) {
                            screenShotsList.add(arrayStr[i]);
                        }
                        
                        return screenShotsList;
                        
                    } else {
                        return null;
                    }
                } else {
                    return null;
                }
            } catch (Exception e) {
                return null;
            }
        }
    }
    
    public static JSONObject getJson(Map<String, Object> map) {
        
        if (map == null) {
            return null;
        }
        
        JSONObject result = new JSONObject();
        Iterator<Entry<String, Object>> it = map.entrySet().iterator();
        
        while (it.hasNext()) {
            
            Entry<String, Object> entry = (Entry<String, Object>) it.next();
            String key = entry.getKey();
            
            if (TextUtils.isEmpty(key)) {
                continue;
            }
            
            Object value = entry.getValue();
            
            if (value instanceof Map) {
                
                JSONObject json = getJson((Map<String, Object>) value);
                
                try {
                    result.put(key, json);
                } catch (Exception e) {
                }
                
            } else if (value instanceof List) {
                
                List<Map<String, Object>> list = (List<Map<String, Object>>) value;
                JSONArray jsonArray = new JSONArray();
                
                for (Map<String, Object> m : list) {
                    
                    JSONObject json = getJson(m);
                    jsonArray.put(json);
                    
                    try {
                        result.put(key, jsonArray);
                    } catch (Exception e) {
                    }
                }
                
            } else {
                
                try {
                    result.put(key, value == null ? "" : value.toString());
                } catch (Exception e) {
                }
            }
        }
        
        return result;
    }
    
}
